﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

namespace ProductionAnalysis.Web
{
    public partial class ProductManage_ProductManage : System.Web.UI.Page
    {
        private ProductType productType;
        private Customer customer;
        public string seachFactor 
        {
            get { return ViewState["SeachFactor"].ToString(); }
            set { ViewState["SeachFactor"] = value; }
        }

        public ProductManage_ProductManage()
        {
            productType = new ProductType();
            customer = new Customer();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                seachFactor = string.Empty;
                BindProductType();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            seachFactor = this.TextBox1.Text.Trim();
            BindProductType();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            seachFactor = string.Empty;
            BindProductType();
        }

        protected void BindProductType()
        {
            if (string.IsNullOrEmpty(seachFactor))
            {
                GridView1.DataSource = productType.LoadEntities();
                GridView1.DataBind();
            }
            else 
            {
                IList<ProductTypeInfo> list = new List<ProductTypeInfo>();
                list.Add(productType.LoadEntity(seachFactor));
                GridView1.DataSource = list;
                GridView1.DataBind();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindProductType();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindProductType();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
                }
            }
            if ((((DropDownList)e.Row.FindControl("DDLCustomerID")) != null) && (((DropDownList)e.Row.FindControl("DDLIsPassive")) != null) && (((DropDownList)e.Row.FindControl("DDLHouseAmount")) != null))
            {
                DropDownList ddlcustomerID = (DropDownList)e.Row.FindControl("DDLCustomerID");
                ddlcustomerID.DataSource = customer.LoadEntities();
                ddlcustomerID.DataTextField = "CustomerName";
                ddlcustomerID.DataValueField = "CustomerID";
                ddlcustomerID.DataBind();
                //
                string customerName = ((HiddenField)e.Row.FindControl("HDFCustomerID")).Value.Trim();
                ddlcustomerID.SelectedValue = customer.GetCustomerbyName(customerName).CustomerID;

                DropDownList ddlisPassive = (DropDownList)e.Row.FindControl("DDLIsPassive");
                ddlisPassive.SelectedValue = ((HiddenField)e.Row.FindControl("HDFIsPassive")).Value.Trim();
                DropDownList ddlhouseAmount = (DropDownList)e.Row.FindControl("DDLHouseAmount");
                ddlhouseAmount.SelectedValue = ((HiddenField)e.Row.FindControl("HDFHouseAmount")).Value.Trim();
            }
            //
            if (((DetailsView)e.Row.FindControl("DetailsView1")) != null)
            {
                DetailsView d = (DetailsView)e.Row.FindControl("DetailsView1");
                if (((DropDownList)d.Rows[1].Cells[1].FindControl("DDLCustomerID")) != null)
                {
                    DropDownList ddlcustomerID1 = (DropDownList)d.Rows[1].Cells[1].FindControl("DDLCustomerID");
                    ddlcustomerID1.DataSource = customer.LoadEntities();
                    ddlcustomerID1.DataTextField = "CustomerName";
                    ddlcustomerID1.DataValueField = "CustomerID";
                    ddlcustomerID1.DataBind();
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string productTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            productType.Delete(productTypeID);
            BindProductType();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindProductType();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {            
            ProductTypeInfo productTypeInfo = new ProductTypeInfo();//(GridView1.DataKeys[e.RowIndex].Values[0].ToString(), GridView1.DataKeys[e.RowIndex].Values[1].ToString(), GridView1.DataKeys[e.RowIndex].Values[2].ToString(), GridView1.DataKeys[e.RowIndex].Values[3].ToString());
            productTypeInfo.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            productTypeInfo.CustomerName = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLCustomerID"))).SelectedValue;
            productTypeInfo.ProductTypeName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
            productTypeInfo.IsPassive = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLIsPassive"))).SelectedValue;
            productTypeInfo.SN3BK = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim().ToString();
            productTypeInfo.MatchingSN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim().ToString();
            productTypeInfo.HouseAmount = int.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLHouseAmount"))).SelectedValue);
            
            productType.Update(productTypeInfo);
            GridView1.EditIndex = -1;
            BindProductType();
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
            ProductTypeInfo productTypeInfo = new ProductTypeInfo();
            productTypeInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
            productTypeInfo.CustomerName = ((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLCustomerID"))).SelectedValue;
            productTypeInfo.ProductTypeName = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
            productTypeInfo.IsPassive = ((DropDownList)(d.Rows[3].Cells[1].FindControl("DDLIsPassive"))).SelectedValue;
            productTypeInfo.SN3BK = ((TextBox)(d.Rows[4].Cells[1].Controls[0])).Text.Trim();
            productTypeInfo.MatchingSN = ((TextBox)(d.Rows[5].Cells[1].Controls[0])).Text.Trim();
            productTypeInfo.HouseAmount = int.Parse(((DropDownList)(d.Rows[3].Cells[1].FindControl("DDLHouseAmount"))).SelectedValue);

            if (string.IsNullOrEmpty(productTypeInfo.ProductTypeID) || string.IsNullOrEmpty(productTypeInfo.ProductTypeName))
            {
                string str1 = "产品类型编号与产品类型名称不能为空，请检查并重新录入！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }

            ProductTypeInfo pro = productType.LoadEntity(productTypeInfo.ProductTypeID);
            ProductTypeInfo pro1 = productType.GetProductTypebyName(productTypeInfo.ProductTypeName);
            if (!string.IsNullOrEmpty(pro.ProductTypeID) && pro.ProductTypeID.Equals(productTypeInfo.ProductTypeID,StringComparison.CurrentCultureIgnoreCase))
            {
                string str1 = "产品类型编号已存在！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
            else
            {
                if (!string.IsNullOrEmpty(pro1.ProductTypeName) && pro1.ProductTypeName.Equals(productTypeInfo.ProductTypeName, StringComparison.CurrentCultureIgnoreCase))
                {
                    string str1 = "产品类型名称已存在！";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                    return;
                }
                else
                {
                    productType.Insert(productTypeInfo);
                    string str1 = "添加成功！";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                    BindProductType();
                }
            }
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            BindProductType();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
}
}